/**************************************************************************************
*  SeqTest.java
*  Program demonstrating the use of threads.
**************************************************************************************
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY,  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL RTJ COMPUTING BE LIABLE FOR ANY CLAIM, DAMAGES
* OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
* Created by:
*      RTJ Computing
**************************************************************************************/
import javax.events.*;
import java.util.*;
import lpc2xxx.hw.*;

class SeqTest
{
    static void main(String[] Args)
    {
        println("\r\nThread sequencing test.");
        println("  Test will create 8 threads to test that the long lasting synchonized");
        println("  method will not disrupt order of threads execution if one or more");
        println("  threads are waiting for lock release.");
        println("\r\nPress any key to start, to terminate test hit ESC now or after the");
        println("last thread is started.\r\n");
        while (true)
        {
            char key = getKey();
            if (key == Console.ESC)
                return;
            else if (key > 0)
                break;
        }

        MyThread t1 = new MyThread(1);
        t1.start();
        MyThread t2 = new MyThread(2);
        t2.start();
        MyThread t3 = new MyThread(3);
        t3.start();
        MyThread t4 = new MyThread(4);
        t4.start();
        MyThread t5 = new MyThread(5);
        t5.start();
        MyThread t6 = new MyThread(6);
        t6.start();
        MyThread t7 = new MyThread(7);
        t7.start();
        MyThread t8 = new MyThread(8);
        t8.start();

        while (true)
        {
            if (getKey() == Console.ESC)
            {
                t1.terminate = true;
                t2.terminate = true;
                t3.terminate = true;
                t4.terminate = true;
                t5.terminate = true;
                t6.terminate = true;
                t7.terminate = true;
                t8.terminate = true;
                break;
            }
            try
            {
                Thread.sleep(200);
            }
            catch (InterruptedException Exc) {}
        }
    }

    static synchronized void longLastingMethod(String s)
    {
        println(s);
        int j = 0;
        for (int i=0; i < 20000; i++)
        {
            j+=i*3;
        }
    }
    static void println(String s)
    {
        Console.println(s);
    }
    static char getKey()
    {
        return Console.getKey();
    }
}

class MyThread extends Thread
{
    int id;
    boolean terminate = false;
    MyThread(int id)
    {
        this.id = id;
        SeqTest.longLastingMethod("Starting Thread "+id);
    }

    public void run()
    {
        while (!terminate)
        {
            SeqTest.longLastingMethod("Thread "+id);
        }
        SeqTest.println("Ending Thread "+id);
    }
}
